package com.itlong.cloud.controller.value;

import com.fasterxml.jackson.core.type.TypeReference;
import com.itlong.cloud.POJO.DTO.operate.OperatePropertySaveDeployDTO;
import com.itlong.cloud.POJO.DTO.operate.OperatePropertyValueSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyValueServerSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyValueServerSaveDeployDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.property.IPropertyValueServerServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      物业-物业增值-缴费规则控制器
 * </desc>
 *
 * @createDate 2019/06/03
 */
@RestController
@RequestMapping("/propertyValue/module")
public class PropertyValuePayRuleController extends BaseController{

    @Autowired
    IPropertyValueServerServiceClient iPropertyValueServerServiceClient;

    /**
     * <desc>
     *      获取页面详情
     * </desc>
     *
     * @param projectId
     * @return
     * @author Jiaqi.X
     * @createDate 2019/05/29
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object getOne(String projectId){
        MessageVO messageVO;
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyValueServerServiceClient.getOne(projectId));
        }catch (Exception e){
            throw new DataAccessException("【物业_物业增值_获取页面详情getOne】获取分页列表失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      保存物业增值数据
     * </desc>
     *
     * @param jsonStr
     * @return
     * @author Jiaqi.X
     * @createDate 2019/05/29
     */
    @RequestMapping(path = "save",method = RequestMethod.POST)
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.UPDATE_PROPERTY_VALUE_RULE_LOG_DESC)
    public Object save(String jsonStr,PropertyValueServerSaveDTO dto){
        MessageVO messageVO;
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            jsonStr = request.getAttribute("jsonStr") == null ? null : request.getAttribute("jsonStr").toString();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyValueServerSaveDTO.class, request);
            dto.setArrearsLevels(JsonUtil.decode(jsonStr, new TypeReference<List<PropertyValueServerSaveDeployDTO>>() {}));
            String result = iPropertyValueServerServiceClient.save(dto);
            messageVO=new MessageVO(result,BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业_物业增值_保存save】保存物业增值数据失败",e);
        }
        return messageVO;
    }
}
